﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.CalculateControls;
using 纸片人.ConvertClass;
using 纸片人.InterfaceFolder;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;
using 纸片人.win2dEffectControl.SliderFloder;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.win2dEffectControl
{
    public abstract class EffectControl : EffectOutputControl, IProvider
    {
        /// <summary>
        /// 效果显示类型名称控件
        /// </summary>
        protected TextBlock effectContrlName;
        /// <summary>
        /// 效果名称控件
        /// </summary>
        protected TextBox effectName;
        /// <summary>
        /// 输出
        /// </summary>
        protected CheckBox cacheOutCheckBox;

        /// <summary>
        /// 输出精度显示控件
        /// </summary>
        protected ComboBox bufferPrecisionComboBox;
        /// <summary>
        /// 接受原图像拖拽控件
        /// </summary>
        protected ProviderDropControl providerDropControl;



        List<CanvasBufferPrecision> listBufferPrecision = new List<CanvasBufferPrecision>();


        #region 事件


        #endregion

        public abstract EffectOneSourceData OneEffetData { get; set; }

        public EffectControl()
        {
            this.DefaultStyleKey = typeof(EffectControl);
            EffectData = OneEffetData;
            listBufferPrecision = EffectUtil.GetEnumList<CanvasBufferPrecision>();

        }

        protected override void OnApplyTemplate()
        {
            EffectData = OneEffetData;

            base.OnApplyTemplate();

            effectContrlName = GetTemplateChild("effectControlName") as TextBlock;

            effectName = GetTemplateChild("effectNameBlock") as TextBox;
            if (effectName != null)
            {
                //创建效果名字绑定
                CreateBinding(Effect, nameof(effectName), new PropertyPath(nameof(ICanvasEffect.Name)), BindingMode.TwoWay, effectName, TextBox.TextProperty);
            }
            cacheOutCheckBox = GetTemplateChild("cacheoutCheck") as CheckBox;
            if (cacheOutCheckBox != null)
            {
                //创建缓存输出绑定
                CreateBinding(Effect, "", new PropertyPath(nameof(ICanvasEffect.CacheOutput)), BindingMode.TwoWay, cacheOutCheckBox, CheckBox.IsCheckedProperty);

            }
            bufferPrecisionComboBox = GetTemplateChild("bufferPrecisionCombo") as ComboBox;
            if (bufferPrecisionComboBox != null)
            {
                bufferPrecisionComboBox.ItemsSource = listBufferPrecision;
                CreateBinding(Effect, "", new PropertyPath(nameof(ICanvasEffect.BufferPrecision)), BindingMode.TwoWay, bufferPrecisionComboBox, ComboBox.SelectedItemProperty);

            }


            SetproviderDropControlElement();
        }


        #region 设置接受原图像下降元素
        /// <summary>
        /// 设置接受原图像下降元素
        /// </summary>
        protected void SetproviderDropControlElement()
        {
            providerDropControl = new ProviderDropControl() { AllowDrop = true, Background = new SolidColorBrush(Color.FromArgb(80, 154, 175, 255)) };
            if (providerDropControl != null)
            {
                providerDropControl.Drop += providerDropControl_Drop;
                providerDropControl.DragEnter += providerDropControl_DragEnter;
                if (controlContainer == null)
                {
                    return;
                }

                var insertIndex = controlContainer.Children.Count == 0 ? 0 : controlContainer.Children.Count - 1;

                controlContainer.Children.Insert(insertIndex, providerDropControl);
            }
        }

        /// <summary>
        /// 被拖拽的元素进入了可接受区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void providerDropControl_DragEnter(object sender, DragEventArgs e)
        {

            if (outputData == null)
            {
                return;
            }
            if (outputData.Type == OutputType.Effect)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }

        }
        /// <summary>
        /// 被拖拽的元素放下了接受区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void providerDropControl_Drop(object sender, DragEventArgs e)
        {
            if (outputData != null && outputData.Type == OutputType.Effect)
            {
                var exporter = (EffectOutputControl)outputData.Exporter;
                if (IsChartLoop(exporter))
                    return;
                CreateConnect(ref oneConnectElement, providerDropControl);
                var exporterData = (EffectData)outputData.ExporterData;
                OneEffetData.SetInputEffectData(exporterData);
            }
        }

        #endregion




        /// <summary>
        /// 删除输入源
        /// </summary>
        public override void RemoveExport(ConnectLineUIElement connectElement)
        {

            //如果这里异常了，则说明，RemoveAllExport 有问题
            oneConnectElement = null;
            var exporter = (EffectOutputControl)connectElement.Exporter;
            OneEffetData.RemoveInputEffect(exporter.EffectData);
            RemoveProvideroneConnectElement(connectElement);
;        }

        /// <summary>
        /// 删除当前控件
        /// </summary>
        public override void Destroy()
        {
          
            base.Destroy();
            oneConnectElement = null;
            EffectData = null;
            OneEffetData = null;
        }



    }
}
